<?php

namespace App\Models\Traits;

use Carbon\Carbon;
use App\Models\User;
use Brick\Math\RoundingMode;

trait WithdrawTrait
{
	public static function isWithdrawOn()
	{
		return (bool)cs('withdraw')->is_open;
	}
	
	public static function allowedLowestAmount()
	{
		return big_decimal(cs('withdraw')->allowed_lowest_amount);
	}

	public static function allowedBankLowestAmount()
	{
		return big_decimal(cs('withdraw')->sandpay_quota);
	}

	public static function allowedStartTime()
	{
		return strtotime(cs('withdraw')->withdraw_start_time);
	}

	public static function allowedEndTime()
	{
		return strtotime(cs('withdraw')->withdraw_end_time);
	}

	public static function allowedSandpayQuota()
	{
		return big_decimal(cs('withdraw')->sandpay_quota);
	}

	public static function isWithdrawTimeAllowed()
	{
		$date= date('H:i:s');
	    $curTime = strtotime($date);//当前时分
	    $allowedStartTime = self::allowedStartTime();//获得指定分钟时间戳，00:00
	    $allowedEndTime = self::allowedEndTime();//获得指定分钟时间戳，01:00
	    $result = false;
	    if($curTime > $allowedStartTime && $curTime < $allowedEndTime){
	        $result = true;
	    }
	    return $result;
	}

	public static function withdrawTimeAllowedRange()
	{
	    $allowedStartTime = self::allowedStartTime();//获得指定分钟时间戳，00:00
	    $allowedEndTime = self::allowedEndTime();//获得指定分钟时间戳，01:00
	   	$allowedStartTime = date("H:i",$allowedStartTime);
	   	$allowedEndTime = date("H:i",$allowedEndTime);
	   	if($allowedStartTime && $allowedEndTime){
	   		$result = $allowedStartTime.'~'.$allowedEndTime;
	   	}else{
	   		$result = "20:30~21:30";
	   	}

	    return $result;
	}
	
	public static function isLowestAmountMatched($coins)
	{
		$allowedLowestAmount = self::allowedLowestAmount();
		if ($allowedLowestAmount->isGreaterThan(0)) {
			return big_decimal($coins)->isGreaterThanOrEqualTo($allowedLowestAmount);
		}
		return true;
	}

	public static function isBankLowestAmountMatched($coins)
	{
		$allowedLowestAmount = self::allowedBankLowestAmount();
		if ($allowedLowestAmount->isGreaterThan(0)) {
			return big_decimal($coins)->isGreaterThanOrEqualTo($allowedLowestAmount);
		}
		return true;
	}
	
	public static function allowedHighestAmount()
	{
		return big_decimal(cs('withdraw')->allowed_highest_amount);
	}
	
	public static function isHighestAmountMatched($coins)
	{
		$allowedHighestAmount = self::allowedHighestAmount();
		if ($allowedHighestAmount->isGreaterThan(0)) {
			return big_decimal($coins)->isLessThanOrEqualTo($allowedHighestAmount);
		}
		return true;
	}
	
	public static function limitedLowestChargeFee()
	{
		return big_decimal(cs('withdraw')->allowed_lowest_charge);
	}
	
	public static function limitedHighestChargeFee()
	{
		return big_decimal(cs('withdraw')->allowed_highest_charge);
	}
	
	public static function chargeType()
	{
		return (string)cs('withdraw')->charge_type;
	}
	
	public static function chargePercent()
	{
		$chargePercent = big_decimal((string)cs('withdraw')->charge_percent);
		if ($chargePercent->isGreaterThanOrEqualTo(0)) {
			return $chargePercent;
		}
		return big_decimal(0);
	}

	public static function XzbChargePercent()
	{
		$chargePercent = big_decimal((string)cs('withdraw')->xzb_charge_percent);
		if ($chargePercent->isGreaterThanOrEqualTo(0)) {
			return $chargePercent;
		}
		return big_decimal(0);
	}
	
	public static function willChargeFee($coins)
	{
		if (self::chargePercent()->isLessThanOrEqualTo(0)) return big_decimal(0);
		
		$fee = big_decimal($coins)->multipliedBy(self::chargePercent()->multipliedBy(0.01));
		
		if (self::limitedLowestChargeFee()->isGreaterThan(0)
			and $fee->isLessThan(self::limitedLowestChargeFee())) {
			return self::limitedLowestChargeFee();
		}
		if (self::limitedHighestChargeFee()->isGreaterThan(0) and
			$fee->isGreaterThan(self::limitedHighestChargeFee())) {
			return self::limitedHighestChargeFee();
		}
		return $fee;
	}

	public static function willChargeFeeByXzb($coins)
	{
		if (self::XzbChargePercent()->isLessThanOrEqualTo(0)) return big_decimal(0);
		
		$fee = big_decimal($coins)->multipliedBy(self::XzbChargePercent()->multipliedBy(0.01));
		
		return $fee;
	}

	public static function willChargeFeeMinusFreeWithdrawQuota(User $user, $coins)
	{
		if (self::chargePercent()->isLessThanOrEqualTo(0)) return big_decimal(0);
		if(big_decimal($coins)->isGreaterThanOrEqualTo($user->freeWithdrawQuota())){
			$fee = big_decimal($coins)->minus($user->freeWithdrawQuota())->multipliedBy(self::chargePercent()->multipliedBy(0.01));
		}else{

			$fee = big_decimal(0);
		}
		return $fee;
	}
	
	public static function isCoinsEnoughToDeduct(User $user, $coins)
	{
		$targetCoins = big_decimal($coins);
		$userCoins = big_decimal($user->balances);
		
		if (self::chargeType() === 'in') {
			return $userCoins->isGreaterThanOrEqualTo($targetCoins);
		}
		if (self::chargeType() === 'out') {
			$allTargetMoney = $targetCoins->plus(self::chargeFee())->toScale(2, RoundingMode::HALF_UP);
			return $userCoins->isGreaterThanOrEqualTo($allTargetMoney);
		}
		return false;
	}

	public static function isBalanceQuotaEnough(User $user, $money)
	{
		$targetMoney = big_decimal($money);
		$thisMonthWithdrawMoney = $user->withdraws->where('created_at','>',Carbon::today()->day(1))->where('status','paid')->sum('actual_amount');

		$allMoney = $targetMoney->plus($thisMonthWithdrawMoney);
		//$userCoins = big_decimal($user->balances);
		
		return $allMoney->isLessThan(800);
		//return false;
	}
	
	public static function willActuallyGetCoins($coins)
	{
		$coins = big_decimal($coins);
		
		if (self::chargeType() === 'in') {
			return $coins->minus(self::willChargeFee($coins));
		} elseif (self::chargeType() === 'out') {
			return $coins;
		}
		return $coins;
	}

	public static function willActuallyGetCoinsByXzb($coins)
	{
		$coins = big_decimal($coins);
		
		if (self::chargeType() === 'in') {
			return $coins->minus(self::willChargeFeeByXzb($coins));
		} elseif (self::chargeType() === 'out') {
			return $coins;
		}
		return $coins;
	}

	public static function willActuallyGetCoinsMinusFreeWithdrawQuota(User $user, $coins)
	{
		$coins = big_decimal($coins);
		
		if (self::chargeType() === 'in') {
			return $coins->minus(self::willChargeFeeMinusFreeWithdrawQuota($user, $coins));
		} elseif (self::chargeType() === 'out') {
			return $coins;
		}
		return $coins;
	}
	
	/*==================================================================================================================*/
	
	
}
